package net.lovesrc.allin.utils;

import sun.misc.BASE64Encoder;

import java.io.*;

public class ImageUtils {


    public static String GetImageStr(File imgFile) {//将图片文件转化为字节数组字符串，并对其进行Base64编码处理
        InputStream in = null;
        byte[] data = null;
        //读取图片字节数组
        try {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        return "data:image/jpeg;base64," + encoder.encode(data);//返回Base64编码过的字节数组字符串
    }

    public static String getValue(File file) {
        InputStream in = null;
        byte[] data = null;
        //读取图片字节数组
        try {
            in = new FileInputStream(file);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            return new String(data, "GBK");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return new String(data);
    }

}
